<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 组件架构内容 -->
      <el-card class="tree-card">
        <TreeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <!-- 结构 -->
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <TreeTools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartment"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <AddDept
      ref="addDepts"
      :tree-node="node"
      :show-dialog.sync="showDialog"
      @addDepts="getDepartment"
    />
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools.vue";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils/index.js";
import AddDept from "./components/add-dept.vue";
export default {
  components: {
    TreeTools,
    AddDept,
  },
  data() {
    return {
      company: { name: "江苏传智播客教育科技股份有限公司", manager: "负责人" },
      departs: [],
      defaultProps: {
        label: "name",
      },
      showDialog: false,
      node: null,
    };
  },
  created() {
    this.getDepartment();
  },
  methods: {
    async getDepartment() {
      const res = await getDepartments();
      this.company = { name: res.companyName, manager: "负责人", id: "" };
      this.departs = tranListToTreeData(res.depts, "");
    },
    // 监听添加部门
    addDepts(node) {
      this.showDialog = true;
      this.node = node;
    },
    editDepts(node) {
      this.showDialog = true;
      this.node = node;
      this.$refs.addDepts.getDepartDetail(node.id);
    },
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>